
      subroutine mediana(vet,nd,rmedian)
!c programmed on 20-mar-1992 by Franco Mele.
      real*4 vet(6000)
      real*4 rmedian,vetm
	  integer nd,i,j,md,id,k
!c ordina il vettore in ordine crescente
! move min to front, as sentinel
k = 1
do i = 2,nd
  if (vet(i) .lt. vet(k)) k = i
enddo
vetm=vet(1)
vet(1)=vet(k)
vet(k)=vetm

! insertion sort with sentinel
do i = 2,nd
       k=i
	   do while(vet(k) .lt. vet(k-1))
         vetm=vet(k-1)
         vet(k-1)=vet(k)
         vet(k)=vetm
		 k=k-1
	   end do   
enddo
!=============

!c per i dispari la mediana e` data dall'elemento centrale;
!c per i pari dalla media dei due centrali.
      md=nd/2
      id=md*2
      if(id.eq.nd)then
        rmedian=(vet(md)+vet(md+1))/2.
      else
        rmedian=vet(md+1)
      endif
      return
      end
